package com.xiaoq.users.repository;

import com.xiaoq.coms.util.IdGeneratorUtils;
import com.xiaoq.users.entity.UserEntity;
import com.xiaoq.users.entity.UserTermDeviceEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
/**
 * 
 * @author wang zhihao
 *
 */
@Repository
public class UserRepository {

	@Autowired
	private Datastore datastore;
	
	private static final Logger LOG = LoggerFactory.getLogger(UserRepository.class);
	
	public UserEntity findByMobile(String mobile){
		UserEntity userIB ;
		userIB = datastore.find(UserEntity.class).filter("mobile",mobile).get();
		return userIB;
		
	}

    public UserEntity findByLoginAccount(String acc) {
    	UserEntity user = datastore.find(UserEntity.class).field("mobile").equal(acc).get();
//    	List<UserEntity> list = datastore.find(UserEntity.class).filter("mobile", acc).asList();
//    	if (list == null || list.size() == 0) {
//    		return null;
//    	}
//    	return list.get(0);
    	return user;
    }

    public UserEntity findByThirdPlatformTypeAndThirdOpenId(String thirdType, String thirdOpenId) {
    	List<UserEntity> list = datastore.find(UserEntity.class).filter("thirdAccountList.thirdAccountType", thirdType)
    			.filter("thirdAccountList.thirdAccount", thirdOpenId).asList();
    	if (list == null || list.size() == 0) {
    		return null;
    	}
    	return list.get(0);
    }

    public UserEntity findByThirdOpenId(String thirdOpenId) {
    	List<UserEntity> list = datastore.find(UserEntity.class).filter("thirdAccountList.thirdAccount", thirdOpenId).asList();
    	if (list == null || list.size() == 0) {
    		return null;
    	}
    	return list.get(0);
    }

    public UserEntity save(UserEntity entity) {
    	datastore.save(entity);
    	return entity;
    }
    
    public UserEntity getById(String id) {
    	return datastore.get(UserEntity.class, id);
    }

	public UserEntity getByTerm(UserTermDeviceEntity term) {
		List<UserEntity> list = datastore.find(UserEntity.class).filter("termDevice.mobileImei", term.getMobileImei()).asList();
    	if (list == null || list.size() == 0) {
    		return null;
    	}
    	return list.get(0);
	}

	public void remove(UserEntity anonymous) {
		datastore.delete(anonymous);
	}

	public List<UserEntity> findAnonymousByTerm(UserTermDeviceEntity termDevice) {
		return datastore.find(UserEntity.class)
				.filter("termDevice.mobileImei", termDevice.getMobileImei())
				.filter("anonymous", true)
				.asList();
	}
	/**
	 * 修改用户信息
	 * @param user
	 * @return
	 */
	public boolean updateUser(UserEntity user){
		Query<UserEntity> userQuery = datastore.find(UserEntity.class);
		UpdateOperations<UserEntity> userUpdate = datastore.createUpdateOperations(UserEntity.class);
		String id = user.getId();
		if(StringUtils.isNotEmpty(id)){
			userQuery.filter(IdGeneratorUtils.IDGENERATOR, id);
			if(userQuery.count()!=0){
				String logoUrl = user.getLogoUrl();
				if(StringUtils.isNotEmpty(logoUrl)){
					userUpdate.set("logoUrl", logoUrl);
				}
				String nickName = user.getNickName();
				if(StringUtils.isNotEmpty(nickName)){
					userUpdate.set("nickName", nickName);
				}
				String mobile = user.getMobile();
				if(StringUtils.isNotEmpty(mobile)){
					userUpdate.set("mobile", mobile);
				}
				String passwd = user.getPasswd();
				if(StringUtils.isNotEmpty(passwd)){
					userUpdate.set("passwd", passwd);
				}
				datastore.update(userQuery, userUpdate);
				return true;
			}
			LOG.info("查无此用户！"+id);
		}else{
			LOG.error("请传递不为空的userId 信息！");
		}
		return false;
	}

	/**
	 *  更新用户的角色,谨慎使用
	 * @param user
	 * @return
	 */
	public boolean updateUserRole(UserEntity user){
		if(user==null || StringUtils.isBlank(user.getId())){
			return false;
		}
		Query<UserEntity> userQuery = datastore.find(UserEntity.class).filter(IdGeneratorUtils.IDGENERATOR, user.getId());
		UpdateOperations<UserEntity> userUpdate = datastore.createUpdateOperations(UserEntity.class);
		userUpdate.set("userRoleList", user.getUserRoleList());
		datastore.update(userQuery, userUpdate);
		return true;
	}

	public List<UserEntity> findByUserIdList(List<String> userIdList){
		if(CollectionUtils.isEmpty(userIdList)){
			return new ArrayList<>();
		}
		Query<UserEntity> userQuery = datastore.find(UserEntity.class)
				.filter("_id in", userIdList);
		return userQuery.asList();

	}

	public List<UserEntity> findAllUser(){
		return datastore.find(UserEntity.class).asList();
	}
}
